﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Logging;
using System.Reflection;

namespace Wicresoft.EKA.ITSM.Business
{


    //    // Scrambles passwords using simple cipher algorithm
    //function getScrambledPassword(pwd) {
    //    var cipher = ['k', 's', 'z', 'h', 'x', 'b', 'p', 'j', 'v', 'c', 'g', 'f', 'q', 'n', 't', 'm'];
    //    var result="";
    //    if (pwd == null)
    //        pwd = "";
    //    pwd = encodeURIComponent(pwd);
    //    //alert("encoded password: " + pwd);
    //    for(var i=0;i<pwd.length;i++) {
    //            var cc = pwd.charCodeAt(i);
    //        result += cipher[Math.floor(cc/16)] + cipher[cc%16];
    //    }
    //    //alert("scrambled password: " + result);
    //    return result;
    //}
    public static class Helper
    {
        private static char[] cipher = { 'k', 's', 'z', 'h', 'x', 'b', 'p', 'j', 'v', 'c', 'g', 'f', 'q', 'n', 't', 'm' };
        private static readonly ILog logger = LogManager.GetCurrentClassLogger();
        public static string Unscramble(string paramString)
        {
            if (paramString == null)
                paramString = "";
            int i = paramString.Length;
            byte[] arrayOfByte = new byte[i];
            int i1 = 0;
            int i2 = 0;
            int i3 = 0;
            for (int j = 0; j + 1 < i; j += 2)
            {
                int m = -1;
                int n = -1;
                try
                {
                    i2 = paramString[j];
                    i3 = paramString[j + 1];
                }
                catch (IndexOutOfRangeException localIndexOutOfBoundsException)
                {
                    logger.Error(localIndexOutOfBoundsException);
                }
                for (int k = 0; k < 16; k++)
                {
                    if (i2 == cipher[k])
                        m = k;
                    if (i3 == cipher[k])
                        n = k;
                }
                arrayOfByte[(i1++)] = ((byte)(m * 16 + n));
            }
            return UTF8Encoding.UTF8.GetString(arrayOfByte).Replace("\0", "");
        }

        public static string Scramble(string pwd)
        {
            var result = "";
            if (pwd == null)
                pwd = "";
            pwd = System.Web.HttpUtility.UrlEncode(pwd);
            //alert("encoded password: " + pwd);
            for (var i = 0; i < pwd.Length; i++)
            {
                var cc = (int)pwd[i];
                result += cipher[(int)(Math.Floor(cc / 16.0))] +""+ cipher[cc % 16];
            }
            //alert("scrambled password: " + result);
            return result;
        }

        public static int MonthtoQuarter(int month)
        {
            var result = 0;
            result = (month - 1) / 3 + 1;
            return result;
        }

        public static List<uint> GetListId<T>()
        {
            Type type = typeof(T);
            List<uint> lists = new List<uint>();
            Object obj = Activator.CreateInstance(type);

            FieldInfo[] fields = obj.GetType().GetFields();
            foreach (FieldInfo eachField in fields)
            {
            }  

            foreach (var item in type.GetProperties())
            {
                //(remainder);
                var b = Convert.ToUInt32(item.CustomAttributes.LastOrDefault().NamedArguments[0].TypedValue.Value.ToString());
                lists.Add(b);
            }
            return lists;
        }

        public static bool choose_net(String userAgent)
        {
            if (userAgent.IndexOf("Noki") > -1 || // Nokia phones and emulators     
                     userAgent.IndexOf("Eric") > -1 || // Ericsson WAP phones and emulators     
                     userAgent.IndexOf("WapI") > -1 || // Ericsson WapIDE 2.0     
                     userAgent.IndexOf("MC21") > -1 || // Ericsson MC218     
                     userAgent.IndexOf("AUR") > -1 || // Ericsson R320     
                     userAgent.IndexOf("R380") > -1 || // Ericsson R380     
                     userAgent.IndexOf("UP.B") > -1 || // UP.Browser     
                     userAgent.IndexOf("WinW") > -1 || // WinWAP browser     
                     userAgent.IndexOf("UPG1") > -1 || // UP.SDK 4.0     
                     userAgent.IndexOf("upsi") > -1 || //another kind of UP.Browser     
                     userAgent.IndexOf("QWAP") > -1 || // unknown QWAPPER browser     
                     userAgent.IndexOf("Jigs") > -1 || // unknown JigSaw browser     
                     userAgent.IndexOf("Java") > -1 || // unknown Java based browser     
                     userAgent.IndexOf("Alca") > -1 || // unknown Alcatel-BE3 browser (UP based)     
                     userAgent.IndexOf("MITS") > -1 || // unknown Mitsubishi browser     
                     userAgent.IndexOf("MOT-") > -1 || // unknown browser (UP based)     
                     userAgent.IndexOf("My S") > -1 ||//  unknown Ericsson devkit browser      
                     userAgent.IndexOf("WAPJ") > -1 ||//Virtual WAPJAG www.wapjag.de     
                     userAgent.IndexOf("fetc") > -1 ||//fetchpage.cgi Perl script from www.wapcab.de     
                     userAgent.IndexOf("ALAV") > -1 || //yet another unknown UP based browser     
                     userAgent.IndexOf("Wapa") > -1 || //another unknown browser (Web based "Wapalyzer")    
                     userAgent.IndexOf("UCWEB") > -1 || //another unknown browser (Web based "Wapalyzer")   
                     userAgent.IndexOf("BlackBerry") > -1 || //another unknown browser (Web based "Wapalyzer")                     
                     userAgent.IndexOf("J2ME") > -1 || //another unknown browser (Web based "Wapalyzer")              
                     userAgent.IndexOf("Oper") > -1 ||
                     userAgent.IndexOf("Android") > -1 ||
                userAgent.IndexOf("mozilla") > -1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
